<?php
class UnitTestCase_Unfuddle_Request extends UnitTestCase_Unfuddle {

    public function testRequestMethodDefaultsToGet() {
    
        $request = new Unfuddle_Request('/foo');
        $this->assertEqual($request->getMethod(), 'GET');
    
    }
    
    public function testRequestUri() {
    
        $request = new Unfuddle_Request('/foo');
        $this->assertEqual($request->getUri(), '/foo');
    
    }
    
    public function testRequestUriEnsuresBeginningSlash() {
    
        $request = new Unfuddle_Request('foo');
        $this->assertEqual($request->getUri(), '/foo');
    
    }
    
    public function testRequestMethod() {
    
        $request = new Unfuddle_Request('/foo', 'POST');
        $this->assertEqual($request->getMethod(), 'POST');
        $request->setMethod('GET');
        $this->assertEqual($request->getMethod(), 'GET');
        $request->setMethod('PUT');
        $this->assertEqual($request->getMethod(), 'PUT');
        $request->setMethod('DELETE');
        $this->assertEqual($request->getMethod(), 'DELETE');
    
    }
    
    public function testRequestSetInvalidMethod() {
    
        $this->expectException(new Unfuddle_Request_Exception('HOST is not a valid request method.'));
        $request = new Unfuddle_Request('/foo', 'HOST');
    
    }
    
    public function testRequestMethodIsCaseInsensitive() {
    
        $request = new Unfuddle_Request('/foo', 'post');
        $this->assertEqual($request->getMethod(), 'POST');
    
    }
    
    public function testQueryParamAccessors() {
    
        $request = new Unfuddle_Request('/foo', 'GET');
        $request->setQueryParam('foo', 'bar');
        $this->assertEqual($request->getQueryParam('foo'), 'bar');
        $this->assertEqual($request->getUri(), '/foo?foo=bar');
    
    }
    
    public function testSetQueryParamsAllAtOnce() {
    
        $params = array('foo' => 'bar', 'baz' => 'var');
        $request = new Unfuddle_Request('/foo/var/baz', 'GET');
        $request->setQueryParams($params);
        $this->assertEqual($request->getQueryParams(), $params);
        $this->assertEqual($request->getUri(), '/foo/var/baz?foo=bar&baz=var');
    
    }
    
    public function testSetHeaders() {
    
        $request = new Unfuddle_Request('/foo', 'GET', array('Content-Type: text/html'));
        $this->assertEqual($request->getHeaders(), array('Content-Type: text/html'));
    
    }
    
    public function testSetHeader() {
    
        $request = new Unfuddle_Request('/foo', 'GET', array('Content-Type: text/html'));
        $request->setHeader('Accept', 'text/plain');
        $this->assertEqual($request->getHeaders(), array('Content-Type: text/html', 'Accept: text/plain'));
    
    }
    
    public function testAddHeader() {
    
        $request = new Unfuddle_Request('/foo', 'GET', array('Accept: text/html'));
        $this->assertEqual($request->getHeader('accept'), 'text/html');
        $request->setHeader('accept', 'application/xml');
        $this->assertEqual($request->getHeader('accept'), 'application/xml');
        $request->addHeader('accept', 'text/html');
        $this->assertEqual($request->getHeader('accept'), array('application/xml', 'text/html'));
    
    }
    
    public function testGetHeader() {
    
        $request = new Unfuddle_Request('/foo', 'GET', array(
            'Referer: http://www.example.com/',
            'Accept: text/plain',
            'Accept: text/html',
            'Accept: text/json',
            'User-Agent: Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101',
        ));
        $this->assertEqual($request->getHeader('referer'), 'http://www.example.com/');
        $this->assertEqual($request->getHeader('accept'), array('text/plain','text/html','text/json'));
    
    }
    
    public function testSetBody() {
    
        $request = new Unfuddle_Request('/foo');
        $request->setBody('foo');
        $this->assertEqual($request->getBody(), 'foo');
    
    }
    
    public function testToString() {
    
        $request = new Unfuddle_Request('/foo', 'POST', array(
            'Referer: http://www.example.com/',
            'User-Agent: Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101',
        ));
        $request->setBody('this is raw post data');
        $requestString = (string) $request;
        $this->assertEqual($requestString, "POST /foo HTTP/1.1\r\nReferer: http://www.example.com/\r\nUser-Agent: Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101\r\n\r\nthis is raw post data");
    
    }

}
